<?php

namespace app\adminapi\logic\reimbursement;

use app\common\logic\BaseLogic;
use app\common\model\reimbursement\ExpenseType;

class ExpenseTypeLogic extends BaseLogic
{
    /**
     * @notes 添加申请类型
     * @param array $params
     * @return bool
     */
    public static function add(array $params): bool
    {
        try {
            $city = new ExpenseType();
            $params['create_time'] = time();
            $city->save($params);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }


    /**
     * @notes 编辑申请类型
     * @param array $params
     * @return bool
     */
    public static function edit(array $params): bool
    {
        try {
            $city = ExpenseType::findOrFail($params['id']);
            $params['update_time'] = time();
            $city->save($params);
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 删除申请类型
     * @param int $id
     * @return bool
     */
    public static function delete(int $id): bool
    {
        try {
            $city = ExpenseType::findOrFail($id);
            $city->delete();
            return true;
        } catch (\Exception $e) {
            self::setError($e->getMessage());
            return false;
        }
    }

    /**
     * @notes 获取所有申请类型列表（用于下拉选择）
     * @param int $id
     * @return array
     */
    public static function getAllExpenseTypes():array
    {
        $fields = ['id', 'name'];
        $expenseTypes = ExpenseType::field($fields)
            ->select()
            ->toArray();
        return $expenseTypes;
    }

    /**
     * @notes 申请类型详情
     * @param int $id
     * @return array
     * @author
     * @date
     */
    public static function detail(int $id): array
    {
        $expenseType = ExpenseType::findOrFail($id);
        return $expenseType->toArray();
    }


}